﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.MembershipProviders;

/// <summary>
/// REpresents selection of projects and teams that are working on them
/// </summary>
public partial class TeamSelection : HierachicalObjectSelection
{
    /// <summary>
    /// Event triggered whem team selection changes
    /// </summary>
    public event EventHandler SelectedTeamChangedEvent;

    /// <summary>
    /// Event triggered when project selection changed
    /// </summary>
    public event EventHandler SelectedProjectChangedEvent;

    /// <summary>
    /// Gets the selected team.
    /// </summary>
    /// <value>The selected team.</value>
    public Team SelectedTeam 
    {
        get 
        {
            if (String.IsNullOrEmpty(this.DropDownListTeams.SelectedValue) || this.DropDownListTeams.SelectedValue == "-1")
            {
                return null;
            }
            else
            {
                return DAOTeam.GetTeam(Int32.Parse(this.DropDownListTeams.SelectedValue));
            }
        } 
    }

    /// <summary>
    /// Gets or sets the selected team.
    /// </summary>
    /// <value>The selected team.</value>
    public int SelectedTeamID
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListTeams.SelectedValue) || this.DropDownListTeams.SelectedValue == "-1")
            {
                return -1;
            }
            else
            {
                return Int32.Parse(this.DropDownListTeams.SelectedValue);
            }
        }
        set
        {
            ListItem li = DropDownListTeams.Items.FindByValue(value.ToString());
            if (li == null)
            {
                Team t = DAOTeam.GetTeam(value);
                int teamID = t == null ? -1 : t.ID;
                Projects.SetProjectByTeam(teamID);
                LoadTeams();
            }
            else
            {
                Projects.SetProjectByTeam(value);
                LoadTeams();
                li.Selected = true;
            }
        }
    }

    /// <summary>
    /// Gets the selected projectID.
    /// </summary>
    /// <value>The selected project ID.</value>
    public int GetSelectedProjectID()
    {
        return this.Projects.SelectedProjectID;
    }

    /// <summary>
    /// Holds selected project ID
    /// </summary>
    public int SelectedProjectID
    {
        get
        {
            return this.Projects.SelectedProjectID;
        }
        set
        {
            Projects.SetProjectID(value);
        }
    }
    /// <summary>
    /// /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        this.Projects.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChanged);
        this.Projects.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChangedKeeper);

        this.Projects.IsFilter = this.IsFilter;
    }

    /// <summary>
    /// It keeps projectchangedevent.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedProjectChangedEvent != null)
            {
                this.SelectedProjectChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// It keeps teamchangedevent.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedTeamChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedTeamChangedEvent != null)
            {
                this.SelectedTeamChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    /// <summary>
    /// Loads the teams.
    /// </summary>
    protected void LoadTeams()
    {
        if (Projects.SelectedProjectID == -1)//String.IsNullOrEmpty(DropDownListProject.SelectedValue))
        {
            DropDownListTeams.DataSource = DAOTeam.GetAllTeams();
        }
        else
        {
            DropDownListTeams.DataSource = DAOTeam.GetAllTeams(DAOProject.GetProjectById(Projects.SelectedProjectID).ID);//Int32.Parse(DropDownListProject.SelectedValue)));
        }
        DropDownListTeams.DataTextField = "Name";
        DropDownListTeams.DataValueField = "ID";
        DropDownListTeams.DataBind();
        if (this.IsFilter)
        {
            DropDownListTeams.Items.Insert(0, TopListItemForFilter("Filters","AnyTeam"));
        }
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        LoadTeams();
    }

    /// <summary>
    /// Selected project changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadTeams();
        }
    }

    /// <summary>
    /// Selected team changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedTeamChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedTeamChangedEvent != null)
            {
                this.SelectedTeamChangedEvent(sender, e);
            }

            if (this.SelectedTeamID == -1)
            {
                return;
            }
            
            int selectedTeamID = this.SelectedTeamID;
            Projects.SetProjectByTeam(selectedTeamID);

            LoadTeams();

            // Select the right team
            for (int i = 0; i < this.DropDownListTeams.Items.Count; i++)
            {
                if (this.DropDownListTeams.Items[i].Value == selectedTeamID.ToString())
                {
                    this.DropDownListTeams.SelectedIndex = i;
                    break;
                }
            }
        }
    }

    /// <summary>
    /// Clears the dropdown lists.
    /// </summary>
    public void Clear()
    {
        DataBind();
        //this.DropDownListProject.SelectedIndex = 0;
        Projects.Clear(this, new EventArgs());
        this.DropDownListTeams.SelectedIndex = 0;
    }

    protected override WebControl GetWebControl()
    {
        return this.DropDownListTeams;
    }

    protected override Label GetLabel()
    {
        return this.LabelTeams;
    }
                
}
